package com.telecom.cms.dao;
// default package

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.*;
import com.telecom.cms.util.Constant;

/**
 * Data access object (DAO) for domain model class CampLeftMenu.
 * 
 * @see .CampLeftMenu
 * @author MyEclipse Persistence Tools
 */

public class CampLeftMenuDAO{
	private static List instlist=null;
	Dao dao = new DaoImpStd();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(CampLeftMenu transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".CAMP_LEFT_MENU where L_Menu_Id="
				+ transientInstance.getLMenuId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".CAMP_LEFT_MENU set Menu_Group_Id="
				        + transientInstance.getMenuGroupId() + ",L_Menu_Name='"
						+ transientInstance.getLMenuName() + "',L_Menu_Link='"
						+ transientInstance.getLMenuLink().replace("'", "''") + "',L_Menu_Show_Order="
						+ transientInstance.getLMenuShowOrder()+ ",L_Menu_Target='"
						+ transientInstance.getLMenuTarget()+"' where L_Menu_Id="
						+ transientInstance.getLMenuId();
			else
				sql = "insert into " + db + ".CAMP_LEFT_MENU values(" 
						+ transientInstance.getLMenuId()+ ","
				        + transientInstance.getMenuGroupId() + " , '"
						+ transientInstance.getLMenuName() + "' , '"
						+ transientInstance.getLMenuLink().replace("'", "''") + "',"
						+ transientInstance.getLMenuShowOrder()+",'" 
						+ transientInstance.getLMenuTarget()+ "')";
			rs = dao.insert(sql);
			if (rs2 != null) {
				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		findAllForce();
		return rs;
	}
	
	public void delete(CampLeftMenu persistentInstance) {
		String sql = "delete from " + db + ".CAMP_LEFT_MENU where L_Menu_Id="
				+ persistentInstance.getLMenuId();
		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		findAllForce();
	}
	
	public List findAllForce() {
		String sql = "select * from " + db + ".CAMP_LEFT_MENU order by L_Menu_Show_order ";
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampLeftMenu sd = new CampLeftMenu();
				sd.setMenuGroupId(rs.getInt(2));
				sd.setLMenuId(rs.getInt(1));
				sd.setLMenuName(rs.getString(3));
				sd.setLMenuLink(rs.getString(4));
				sd.setLMenuShowOrder(rs.getInt(5));
				sd.setLMenuTarget(rs.getString(6));
				list.add(sd);
			}
			if (rs != null) {
				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		instlist=list;
		return list;
	}
	
	public List findAll() {
		if(instlist==null)
			findAllForce();
		return instlist;
	}
	
	public List findAllByGroupId(int menugroupid) {
		//String sql = "select * from " + db + ".CAMP_LEFT_MENU where Menu_Group_Id="+menugroupid+" order by L_Menu_Show_order ";
		List list = new ArrayList();
		if(instlist==null)
			findAllForce();
		//System.out.println(instlist.size());
		for(int i=0;i<instlist.size();i++)
		{
			CampLeftMenu sd = (CampLeftMenu)instlist.get(i);
			//System.out.println(sd.getMenuGroupId()+"=?="+menugroupid);
			if(sd.getMenuGroupId()==menugroupid)
				list.add(sd);
		}
		
		/*try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampLeftMenu sd = new CampLeftMenu();
				sd.setMenuGroupId(rs.getInt(2));
				sd.setLMenuId(rs.getInt(1));
				sd.setLMenuName(rs.getString(3));
				sd.setLMenuLink(rs.getString(4));
				sd.setMenuGroupId(rs.getInt(5));
				sd.setLMenuTarget(rs.getString(6));
				list.add(sd);
			}
			if (rs != null) {
				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}*/
		return list;
	}
}